package com.hrbxlh.actionImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.hrbxlh.dao.MyCourseDao;
import com.hrbxlh.util.OpenstackUtil;
import com.hrbxlh.util.SessionValues;

/**
* -------------------------------------------------------
* Copyright (c) 2017, 鑫联华信息科技股份有限公司
* All rights reserved.
* FileName：MyCourseActionImpl.java
* Description：我的课程actionImpl类,负责查询登录人的课程信息。
* History：
* Date           Author               Desc
* 2017.4.5		  xzx                 建立
* -------------------------------------------------------
*/

@Component("myCourseActionImpl")
@Transactional
public class MyCourseActionImpl {

	@Resource(name="myCourseDao")
	private MyCourseDao myCourseDao;

	/**
	 * @description 获取登录人的课程信息，（学生、教师）
	 * @param Map<String,Object> paramMap  人员id，角色id
	 * @return List<Map<String,Object>>
	 * @throws Exception
	 */
	public List<Map<String, Object>> getMyCourse(Map<String, Object> paramMap) throws Exception{
		
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		try{
			//如果是学生
			if((Integer)paramMap.get("roleId") == 1){
				list = myCourseDao.getStuCourse(paramMap);
			//如果是教师	
			}else if((Integer)paramMap.get("roleId") == 2){
				list = myCourseDao.getTeacherCourse(paramMap);
			}
		}catch(Exception e){
			throw e; 
		}
		return list;
	}
	
	/**
	 * @description 获取课程表信息
	 * @param Map<String,Object> paramMap 工号
	 * @return List<Map<String, Object>>  课程表集合
	 * @throws Exception
	 */
	public List<Map<String, Object>> getCouseTab(Map<String, Object> paramMap) throws Exception{
		
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		try{
			//如果是学生
			if((Integer)paramMap.get("roleId") == 1){
				list = myCourseDao.getStuCouseTab(paramMap);
			//如果是教师	
			}else if((Integer)paramMap.get("roleId") == 2){
				//查询老师教的一种类型的课
				List<Map<String, Object>> oneList= myCourseDao.getTeaTeachCourseId(paramMap);
				if(oneList != null && oneList.size() != 0){
					paramMap.put("oneList", oneList);
					list = myCourseDao.getTeacherCouseTab(paramMap);
				}
				//查询老师教的正常课还有选课的课类型的
				paramMap.put("type", 3);
				List<Map<String, Object>> threeList= myCourseDao.getTeaTeachCourseId(paramMap);
				if(threeList != null && threeList.size() != 0){
					paramMap.put("threeList", threeList);
					List<Map<String, Object>> allTeachList = myCourseDao.getTeacherCouseTabAll(paramMap);
					if(allTeachList != null && allTeachList.size() != 0){
						if(list != null && list.size() != 0){
							list.addAll(allTeachList);
						}else{
							list = allTeachList;
						}
					}
				}
			}
		}catch(Exception e){
			throw e; 
		}
		return list;
	}
	
	/**
	 * @description 获取课程表信息(上一周下一周的)
	 * @param Map<String,Object> paramMap 工号 角色
	 * @return List<Map<String, Object>>  课程表集合
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> getTabList(Map<String, Object> paramMap) throws Exception{
		
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		try{
			List<String> listUse = (List<String>)paramMap.get("dateList");
			paramMap.put("startDate", listUse.get(0));
			paramMap.put("endDate", listUse.get(listUse.size()-1));
			//如果是学生
			if((Integer)paramMap.get("roleId") == 1){
				list = myCourseDao.getStuCourseTabSearch(paramMap);
			//如果是教师	
			}else if((Integer)paramMap.get("roleId") == 2){
				//查询老师教的一种类型的课
				List<Map<String, Object>> oneList= myCourseDao.getTeaTeachCourseId(paramMap);
				if(oneList != null && oneList.size() != 0){
					paramMap.put("oneList", oneList);
					list = myCourseDao.getTeacherCourseTabSearch(paramMap);
				}
				//查询老师教的正常课还有选课的课类型的
				paramMap.put("type", 3);
				List<Map<String, Object>> threeList= myCourseDao.getTeaTeachCourseId(paramMap);
				if(threeList != null && threeList.size() != 0){
					paramMap.put("threeList", threeList);
					List<Map<String, Object>> allTeachList = myCourseDao.getTeacherCourseTabSearchAll(paramMap);
					if(allTeachList != null && allTeachList.size() != 0){
						if(list != null && list.size() != 0){
							list.addAll(allTeachList);
						}else{
							list = allTeachList;
						}
					}
				}
				//list = myCourseDao.getTeacherCourseTabSearch(paramMap);
			
			}
		}catch(Exception e){
			throw e; 
		}
		return list;
	}
	
	/**
	 * @description 获取学生的课程
	 * @param Map<String,Object> paramMap  人员id
	 * @return List<Map<String,Object>>
	 * @throws Exception
	 */
	public List<Map<String, Object>> getStuCourse(Map<String, Object> paramMap) throws Exception{
		
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		try{	
			list = myCourseDao.getStuCourse(paramMap);
		}catch(Exception e){
			throw e; 
		}
		return list;
	}

	/**
	 * @description 获取登录人的虚机信息
	 * @param Map<String,Object> paramMap  人员id
	 * @return List<Map<String,Object>>
	 * @throws Exception
	 */
	public List<Map<String, Object>> getMyvirtualAddress(Map<String, Object> paramMap) throws Exception{
		
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		try{	
			//获取我的虚拟机信息，需要用是不是主机的字段进行排序，主机在上从在下
			list = myCourseDao.getMyvirtualAddress(paramMap);
			//通过虚机id查询虚机的地址
			for(Map<String, Object> map : list){
				String vitualId = (String)map.get("vmId");
				String url = OpenstackUtil.queryConsole(vitualId);
				map.put("url",url);
			} 
		}catch(Exception e){
			throw e; 
		}
		return list;
	}
	
	/**
	 * @description /获取这个学生这个课，这个章节的试验评价
	 * @param Map<String,Object> paramMap  课程id  章节id  登录人id  登录人角色
	 * @return List<Map<String,Object>>
	 * @throws Exception
	 */
	public Map<String, Object> getMyTestAssign(Map<String, Object> paramMap, HttpServletRequest request) throws Exception{
		
		Map<String, Object> map = new HashMap<String,Object>();
		try{	
			//获取登录人角色信息
			SessionValues session = new SessionValues();
			Map<String, Object> sessionValues = session.getSessionValues(new String[]{"roleId","stuorteaId"}, request);
			//如果是学生
			if(Integer.parseInt(sessionValues.get("roleId").toString()) == 1){
				paramMap.put("stuorteaId", sessionValues.get("stuorteaId"));
				//查询这个课，这个章节，这个学生的试验提交情况
				map = myCourseDao.getMyTestAssign(paramMap);
			}
		}catch(Exception e){
			throw e; 
		}
		return map;
	}
	
}